//
//  BCProjectDetailIntroductionCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/22.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectDetailIntroductionCell.h"

#import <OCSkeleton.h>

static CGFloat BCProjectDetailIntroductionCellTopPadding = 2.0;
static CGFloat BCProjectDetailIntroductionCellBottomPadding = 10.0;

@interface BCProjectDetailIntroductionCell () <OCGradientsOwner>

@property (nonatomic, strong) UILabel *intruductionLabel;

/**
 *paragraphStyle
 */
@property (nonatomic, strong) NSMutableParagraphStyle *paragraphStyle;


@property (nonatomic, strong) OCGradientContainerView *placeholderContainerView;

@end

@implementation BCProjectDetailIntroductionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fd_enforceFrameLayout = true;
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}
- (void)setupSubviews {
    self.intruductionLabel = [UILabel new];
    self.intruductionLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.intruductionLabel.numberOfLines = 0;
    self.intruductionLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.intruductionLabel];
    [self.intruductionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(BCProjectDetailCellLeftPadding);
        make.right.mas_equalTo(self.contentView).offset(-BCProjectDetailCellRightPadding);
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailIntroductionCellTopPadding);
    }];
}
- (void)setContainerViews {
    self.placeholderContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.placeholderContainerView];
    [self.placeholderContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.intruductionLabel);
        make.top.mas_equalTo(self.intruductionLabel);
        make.right.mas_equalTo(self.intruductionLabel);
        make.height.mas_equalTo(self.intruductionLabel.font.lineHeight);
    }];
}


- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat height = 0;
    height += BCProjectDetailIntroductionCellTopPadding;
    if (self.introductionModel) {
        CGSize textSize = [self.intruductionLabel sizeThatFits:CGSizeMake(size.width-BCProjectDetailCellLeftPadding-BCProjectDetailCellRightPadding, CGFLOAT_MAX)];
        height += textSize.height;
    }else {
        height += self.intruductionLabel.font.lineHeight;
    }
    height += BCProjectDetailIntroductionCellBottomPadding;
    return CGSizeMake(size.width, height);
}

- (NSMutableParagraphStyle *)paragraphStyle {
    if (!_paragraphStyle) {
        _paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [_paragraphStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
        _paragraphStyle.lineSpacing = 6;
    }
    return _paragraphStyle;
}


- (void)setIntroductionModel:(BCProjectDetailIntroductionModel *)introductionModel {
    _introductionModel = introductionModel;
    //
    if (introductionModel) {
        self.placeholderContainerView.alpha = 0;
        self.placeholderContainerView.backgroundColor = [UIColor clearColor];
        
        self.intruductionLabel.attributedText = [[NSAttributedString alloc] initWithString:introductionModel.introduction attributes:@{NSParagraphStyleAttributeName:self.paragraphStyle}];
    }else {
        self.intruductionLabel.attributedText = nil;
        self.placeholderContainerView.alpha = 1;
        self.placeholderContainerView.backgroundColor = [OCGradientContainerView appearance].backgroundColor;
    }
}


#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    if (self.placeholderContainerView) {
        return @[self.placeholderContainerView.gradientLayer];
    }
    return nil;
}

- (NSArray <UIView *>*)skeletonViews {
    if (self.placeholderContainerView) {
        return @[self.placeholderContainerView];
    }
    return nil;
}


@end
